import { app, shell, BrowserWindow, ipcMain, clipboard  } from 'electron';
import { join } from 'path';
import { electronApp, optimizer, is } from '@electron-toolkit/utils';
import icon from '../../resources/icon.png?asset';
// @ts-ignore (define in dts)
import getMac from "getmac";
import {createTray} from '../preload/createTray'

// 创建实例 主窗口
function createWindow(): void {
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 900,
    show: false,
    autoHideMenuBar: true,
    ...(process.platform === 'linux' ? { icon } : {icon}),
    webPreferences: {
      preload: join(__dirname, '../preload/index.js'),
      sandbox: false,
      webSecurity: true,
      nodeIntegration: false
    }
  });

  mainWindow.on('ready-to-show', () => {
    mainWindow.show();
  });

  mainWindow.webContents.setWindowOpenHandler((details) => {
    shell.openExternal(details.url);
    return { action: 'deny' };
  });

  if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
    mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL']);
  } else {
    mainWindow.loadFile(join(__dirname, '../renderer/index.html'));
  }
  // 开发调试
  // mainWindow.webContents.openDevTools()
}

app.whenReady().then(() => {
  // Set app user model id for windows
  electronApp.setAppUserModelId('com.electron');
  app.on('browser-window-created', (_, window) => {
    optimizer.watchWindowShortcuts(window)

  });
  // IPC test
  ipcMain.on('ping', () => console.log('pong'));

  ipcMain.handle('get_ip_address', () => {
    const address = getIpAddress();
    return address;
  });

  ipcMain.handle('get_copy', () => {
    getCopy();
  });

  createTray();
  createWindow();

  app.on('activate', function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });

  // const ipAddress = getIpAddress();
});


app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});


function getIpAddress() {
  // @ts-ignore (define in dts)
  const id = getMac()
  console.log('id', getMac());


  return id;
}


function getCopy() {
  const text = getIpAddress()
  clipboard.writeText(text);
}

function getPcMsg() {
  let interfaces = require('os').networkInterfaces()
  let pcObj = {}
  let pcMessage = []
  for (let key in interfaces) {
      if(key.indexOf('WLAN') !== -1 || key.indexOf('无线网络连接') !== -1) {
          pcObj = interfaces[key]
          break
      } else if (key.indexOf('以太网') !== -1 || key.indexOf('本地连接') !== -1) {
          pcObj = interfaces[key]
      } else if (Object.keys(pcObj).length < 1) {
          pcObj = interfaces[key]
      }
  }
  // @ts-ignore (define in dts)
  pcMessage = pcObj.filter((item) => {
      if(item.family === 'IPv4') {
          return item
      }
  })
  return pcMessage[0];
}

function getMac(){
  // 引入第三方库
  // let Mac = getMac();
  // @ts-ignore (define in dts)
  let Mac = getPcMsg().mac;
 return Mac
 
}
